package com.kungfuhacking.wristbandpro.location.presenter;

import android.content.SharedPreferences;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.location.bean.OldEntity;
import com.kungfuhacking.wristbandpro.location.view.IPickView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.my.activity.AddOldActivity;
import com.kungfuhacking.wristbandpro.net.BaseListObserver;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/5/3.
 */

public class IPickPresenter implements BasePresenter {

    private IPickView iPickView;
    private List<OldEntity> oldEntities = new ArrayList<>();

    public IPickPresenter(IPickView iPickView) {
        this.iPickView = iPickView;
    }

    /**
     * 获取所有老人的信息
     */
    public void getAllOldInfo() {

        JSONObject j = new JSONObject();
        j.put("token", iPickView.getToken());

        RetrofitHttpUtil.getInstance().getOldAndWatch(new BaseListObserver<OldEntity>() {
            @Override
            public void onHandleSuccess(List<OldEntity> t) {
                iPickView.hideLoading();

                if (t == null) {
                    iPickView.showErr("请先绑定老人");
                    return;
                }

                //先清空,再添加全部的
                oldEntities.clear();
                oldEntities.addAll(t);

                if (t.size() == 0)
                    go2AddOldAndClearData();
                else
                    iPickView.setOldList(t);

            }

            @Override
            public void onHandleError(String message) {
                iPickView.hideLoading();
                iPickView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iPickView.showErr(message);
                iPickView.hideLoading();
                iPickView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iPickView.showLoading("获取中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    /**
     * 跳转到添加老人页面,并且清除老人记录
     */
    private void go2AddOldAndClearData() {

        SharedPreferences.Editor edit = iPickView.getSp().edit();
        edit.putString(Constant.CURRENTOLDID, "");
        edit.putString(Constant.CURRENTOLDNAME, "");
        edit.putInt(Constant.LEVEL, 0);
        edit.apply();

        Bundle bundle = new Bundle();
        bundle.putBoolean("cancle", false);
        iPickView.nextActivityWithBundle(AddOldActivity.class, bundle);
    }

    /**
     * 删除老人
     *
     * @param oldId    老人的id
     * @param position
     */
    public void deleteOld(final String oldId, final int position) {

        JSONObject j = new JSONObject();
//        j.put("id", 23456);
        j.put("id", oldId);
        j.put("token", iPickView.getToken());

        RetrofitHttpUtil.getInstance().delOld(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                iPickView.hideLoading();
                iPickView.showErr("删除成功");

                //如果删之前只有一个老人,删除成功后就没有老人了. 跳到添加老人页面
                if (oldEntities.size() == 1)
                    go2AddOldAndClearData();
                else
                    iPickView.updateDeleteInfo(position);

            }

            @Override
            public void onHandleError(String message) {
                iPickView.hideLoading();
                iPickView.showErr(message);
                //删除失败
                iPickView.setOldList(oldEntities);
            }

            @Override
            public void onReLogin(String message) {
                iPickView.showErr(message);
                iPickView.hideLoading();
                iPickView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iPickView.showLoading("删除中...");
            }
        }, new RequestBean().setData(j.toString()));
    }

    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
